/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.server.game;

import com.core.log.Debug;
import com.core.utils.Utils;
import java.util.Random;

/**
 *
 * @author Administrator
 */
public class ShareCard {

    private static Random random = new Random();
    private static int NUMBER_CARDS = 52;
    private int[] ACE_CARDS = {39, 26, 13, 0};
    private int[] TWO_CARDS = {40, 27, 14, 1};
    private int[] KING_CARDS = {51, 38, 25, 12};
//    private static int[] voidPosition;
//    private static int[] insertCardId;
//    private int voidPosIndex;
//    private int insertCardIndex;
    private int aceCardIndex;
    private int twoCardIndex;
    private int kingCardIndex;
//    private int insertFourPairIndex;
    private int[] cardId;

    public static void main(String[] args) {
        ShareCard dealer = new ShareCard();
        dealer.initCard();
        dealer.insertTwoCard(3);
        dealer.getCardId();
        dealer.show();
        dealer.sort();
        dealer.show();
    }

    public ShareCard() {
    }

    private void mergeArr(int[] arr, int pos1, int pos2) {
        int temp = arr[pos1];
        arr[pos1] = arr[pos2];
        arr[pos2] = temp;
    }

    public void initCard() {
        aceCardIndex = 0;
        twoCardIndex = 0;
        kingCardIndex = 0;
        cardId = new int[52];
        for (int i = 0; i < 52; i++) {
            int pos = Utils.nextInt(4);
            switch (pos) {
                case 0:
                    mergeArr(ACE_CARDS, pos, i % 4);
                    break;
                case 1:
                    mergeArr(KING_CARDS, pos, i % 4);
                    break;
                case 2:
                    mergeArr(TWO_CARDS, pos, i % 4);
                    break;
            }
            cardId[i] = i;
        }
        if (Debug.isTest) {
            cardId = new int[]{
                10, 49, 20, 6, 43, 17, 27, 0, 18, 3, 24, 29, 7,
                2, 15, 28, 16, 25, 36, 31, 34, 11, 44, 41, 46, 40,
                48, 39, 51, 47, 32, 45, 8, 9, 37, 42, 1, 23, 14, 19,
                33, 4, 21, 22, 12, 38, 26, 50, 5, 30, 13, 35, 37
            };
        }
    }

    protected int[] getCardId() {
        if (!Debug.isTest) {
            mergeCard();
            insertCard();
        }
        return cardId;
    }

    protected void insertTwoCard(int position) {
        //cho vi tri 34=26
        cardId[TWO_CARDS[twoCardIndex]] = position;
        cardId[position] = -2;
        twoCardIndex++;
    }

    protected void insertAceCard(int position) {
        //cho vi tri 34=26
        cardId[ACE_CARDS[aceCardIndex]] = position;
        cardId[position] = -1;
        aceCardIndex++;
    }

    protected void insertKing(int position) {
        //cho vi tri 34=26
        cardId[KING_CARDS[kingCardIndex]] = position;
        cardId[position] = -3;
        kingCardIndex++;
    }

    public void mergeCard() {
        for (int i = 0; i < 156; i++) {
            int pos1 = i % 52;
            int pos2 = nextInt(NUMBER_CARDS);
            if (cardId[pos1] == -1 || cardId[pos2] == -1) {
                continue;
            }
            if (cardId[pos1] == -2 || cardId[pos2] == -2) {
                continue;
            }
            if (cardId[pos1] == -3 || cardId[pos2] == -3) {
                continue;
            }
            int tg = cardId[pos1];
            cardId[pos1] = cardId[pos2];
            cardId[pos2] = tg;
        }
    }

    public static int nextInt(int limit) {
        return random.nextInt(limit);
    }

    private void insertCard() {

        aceCardIndex = 0;
        twoCardIndex = 0;
        kingCardIndex = 0;
        for (int i = 0; i < NUMBER_CARDS; i++) {
            if (cardId[i] == -1) {
                cardId[i] = ACE_CARDS[aceCardIndex];
                aceCardIndex++;
            }
            if (cardId[i] == -2) {
                cardId[i] = TWO_CARDS[twoCardIndex];
                twoCardIndex++;
            }
            if (cardId[i] == -3) {
                cardId[i] = KING_CARDS[kingCardIndex];
                kingCardIndex++;
            }
        }
    }

    private void show() {
        for (int i = 0; i < 52; i++) {
            //   System.out.println(i + ":" + cardIds[i] + ",");
            System.out.print(cardId[i] + ",");
            if (i != 0 && i % 13 == 12) {
                System.out.println();
            }
        }
        System.out.println();
    }

    private void sort() {
        for (int i = 0; i < cardId.length - 1; i++) {
            for (int j = i; j < cardId.length; j++) {
                if (cardId[i] > cardId[j]) {
                    int tg = cardId[i];
                    cardId[i] = cardId[j];
                    cardId[j] = tg;
                }
            }
        }
    }
}
